package com.pactera.asmp.server.job.dispatch.impl.feedback;

import com.pactera.asmp.server.common.constants.OccupyStatus;
import com.pactera.asmp.server.common.constants.SubTaskStatus;
import com.pactera.asmp.server.common.logprint.utils.CollectionUtils;
import com.pactera.asmp.server.dao.DeviceInfoMapper;
import com.pactera.asmp.server.dao.SubtaskMapper;
import com.pactera.asmp.server.dao.TaskDeviceMapper;
import com.pactera.asmp.server.dao.TaskMapper;
import com.pactera.asmp.server.entity.*;
import com.pactera.asmp.server.pojo.DeviceInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 为了升级多机部署
 */
@Component
public class FeedbackDataRepository {

    @Autowired
    private TaskMapper taskMapper;

    @Autowired
    private TaskDeviceMapper taskDeviceMapper;

    @Autowired
    private DeviceInfoMapper deviceInfoMapper;

    @Autowired
    private SubtaskMapper subtaskMapper;

    public DeviceInfo getDeviceByDuid(String duid){
        return deviceInfoMapper.selectByDuid(duid);
    }

    public int updateStatusByDuid(DeviceInfo deviceInfo){
        return deviceInfoMapper.updateStatusByDuid(deviceInfo.getDuid(), deviceInfo.getStatus(), LocalDateTime.now());
    }

    public List<Subtask> getCancelSubtaskByTaskIdAndDuid(String duid, long taskId){
        List<Subtask> list = new ArrayList<>();
        SubtaskExample example = new SubtaskExample();
        SubtaskExample.Criteria criteria = example.createCriteria().andTaskIdEqualTo(taskId).andDuidEqualTo(duid);
        List<Subtask> subtasks = subtaskMapper.selectByExample(example);
        if(CollectionUtils.isNotEmpty(subtasks)){
            for(Subtask subtask : subtasks){
                if(subtask.getTaskStatus().equals(SubTaskStatus.FAIL_WAIT_CANCEL.getValue())){
                    list.add(subtask);
                }
            }
        }
        return list;
    }


    public List<TaskDevice> getTaskDeviceListByDuid(String duid){
        TaskDeviceExample taskDeviceExample = new TaskDeviceExample();
        taskDeviceExample.createCriteria().andDuidEqualTo(duid).andOccupyStatusEqualTo(OccupyStatus.OCCUPIED.getValue()).andIsDeletedEqualTo(0);
        return taskDeviceMapper.selectByExample(taskDeviceExample);
    }

    public List<TaskDevice> getTaskDeviceByTaskIdAndDuid(Long taskId, String duid){
        TaskDeviceExample taskDeviceExample = new TaskDeviceExample();
        taskDeviceExample.createCriteria().andTaskIdEqualTo(taskId).andDuidEqualTo(duid).andIsDeletedEqualTo(0);
        return taskDeviceMapper.selectByExample(taskDeviceExample);
    }

    @Transactional
    public boolean modifyDeviceStatus(TaskDevice taskDevice){
        TaskDTO taskDTO = taskMapper.getTaskHistoryById2(taskDevice.getTaskId().intValue());
        deviceInfoMapper.updateAutoTestStatus1(taskDevice.getDuid(), taskDTO.getUserId(), taskDTO.getUserName());
        if (!StringUtils.isEmpty(taskDevice.getAuxDuid())) {
            deviceInfoMapper.updateAutoTestStatus1(taskDevice.getAuxDuid(), taskDTO.getUserId(), taskDTO.getUserName());
        }
        return true;
    }
    
}
